package com.atguigu.guli.service.ucenter.service.impl;

import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.common.base.util.FormUtils;
import com.atguigu.guli.common.base.util.JwtInfo;
import com.atguigu.guli.common.base.util.JwtUtils;
import com.atguigu.guli.common.base.util.MD5;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.vo.LoginVo;
import com.atguigu.guli.service.ucenter.entity.vo.RegisterVo;
import com.atguigu.guli.service.ucenter.mapper.MemberMapper;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author liujun
 * @since 2021-02-10
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void register(RegisterVo registerVo) {
        //校验参数
        if (StringUtils.isEmpty(registerVo.getCode()) ||
            StringUtils.isEmpty(registerVo.getMobile()) ||
            StringUtils.isEmpty(registerVo.getNickname()) ||
            StringUtils.isEmpty(registerVo.getPassword()) ||
            !FormUtils.isMobile(registerVo.getMobile())) {
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
            }
        //校验验证码
        final String checkCode = (String)redisTemplate.opsForValue().get(registerVo.getMobile());
        if(!StringUtils.equals(registerVo.getCode(),checkCode)){
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }
        //校验用户手机号是否注册
        final int res = this.count(new QueryWrapper<Member>().eq("mobile", registerVo.getMobile()));
        if(res>0){
            throw new GuliException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }
        //注册
        Member member = new Member();
        member.setNickname(registerVo.getNickname());
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(registerVo.getPassword()));
        member.setDisabled(false);
        member.setAvatar("https://guli-file-helen.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");
        this.save(member);
    }

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //校验参数
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)) {
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }
        //校验手机号
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Member member = baseMapper.selectOne(queryWrapper);
        if(member == null){
            throw new GuliException(ResultCodeEnum.LOGIN_MEMBER_ERROR);
        }
        //校验密码
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //检验用户是否被禁用
        if(member.getDisabled()){
            throw new GuliException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());
        String jwtToken = JwtUtils.getJwtToken(jwtInfo, 1800);
        return jwtToken;
    }

    @Override
    public Member getByOpenid(String openid) {

        final List<Member> members = this.list(new QueryWrapper<Member>().eq("openid", openid));
        if(CollectionUtils.isEmpty(members)){
            return null;
        }
        return members.get(0);
    }

    @Override
    public Integer countRegisterNum(String day) {

        return baseMapper.selectRegisterNumByDay(day);
    }
}
